package com.wuchaooooo.service.zjutsds.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuchaooooo.service.zjutsds.pojo.po.PUserAnswer;
import com.wuchaooooo.service.zjutsds.pojo.vo.VCareer;
import com.wuchaooooo.service.zjutsds.pojo.vo.VMajor;
import com.wuchaooooo.service.zjutsds.pojo.vo.VQuestion;
import com.wuchaooooo.service.zjutsds.pojo.vo.VResult;
import com.wuchaooooo.service.zjutsds.pojo.vo.VUser;
import com.wuchaooooo.service.zjutsds.service.QuestionsService;
import com.wuchaooooo.service.zjutsds.service.UserService;
import com.wuchaooooo.service.zjutsds.utils.AuthUtils;

/**
 * Created by wuchaooooo on 07/06/2017.
 */
@Controller
public class QuestionsController {
    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/questions", method = RequestMethod.GET)
    public String listQuestions(
            Map<String, Object> model) {
        VUser vUser = userService.getUser(AuthUtils.getAuthUser().getUserName());
        int isActivated = vUser.getIsActivated();
        if (isActivated == 0) {
            return "questions-notactive";
        }
        Map<String, List<VQuestion>>  map= questionsService.listQuestion();
        model.put("capQuestion", map.get("capability"));
        model.put("hobQuestion", map.get("hobby"));
        

        PUserAnswer ua= questionsService.getQuestion(vUser.getId());
        if (StringUtils.isEmpty(vUser.getCharacter())) {
            
            model.put("ua", ua);
            return "questions";
        } else {
        	VResult vResult = new VResult();
        	BeanUtils.copyProperties(ua, vResult);
        	Map<String, Object> result = questionsService.calcAndRecom(vResult, vUser);
            model.put("result", result);
        	return "redirect:/result";
        }
    }

    @RequestMapping(value = "/questions", method = RequestMethod.POST)
    public String postQuestions(VResult vResult, Map<String, Object> model) {
    	
    	questionsService.postResult(vResult, AuthUtils.getAuthUser());
    	
        return "redirect:/result";
    }
    
    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public String postQuestions(Map<String, Object> model) {
    	VUser vUser = userService.getUser(AuthUtils.getAuthUser().getUserName());
    	PUserAnswer ua= questionsService.getQuestion(vUser.getId());
        if (StringUtils.isEmpty(vUser.getCharacter())) {
        	return "redirect:/questions";
        } else {
        	VResult vResult = new VResult();
        	BeanUtils.copyProperties(ua, vResult);
        	Map<String, Object> result = questionsService.calcAndRecom(vResult, vUser);
            model.putAll(result);
            model.put("user", vUser);
            return "result";
        }
    }
    
    @RequestMapping(value = "/saveQuestions", method = RequestMethod.POST)
    @ResponseBody
    public String saveQuestions(VResult vResult, Map<String, Object> model) {
    	
        int count = questionsService.saveQuestion(vResult, AuthUtils.getAuthUser().getId());
        return "success";
    }

    //---------------------------------------
    /*    
    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    public String demo(Map<String, Object> model) {
        VCareer vCareer = null;//questionsService.getCareer("EIS");
        List<VMajor> vMajorList = questionsService.listMajor("EIS");
        model.put("career", vCareer);
        model.put("majors", vMajorList);
        return "result";
    }

    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public String getResult(Map<String, Object> model) {
        VUser vUser = userService.getUser(AuthUtils.getAuthUser().getUserName());
        String sdsName = vUser.getSdsName();
        if (sdsName == null) {
            return "result-null";
        }
        VCareer vCareer = questionsService.getCareer(vUser);
        List<VMajor> vMajorList = questionsService.listMajor(sdsName);
        model.put("career", vCareer);
        model.put("majors", vMajorList);
        return "result";
    }*/

}
